import { EFormItemType, EFormItemShowType, EFormItemContainer } from "./enum";
import { IFormItem, IFormOptions } from "./type";

export class FormItem implements IFormItem {
  key: string;
  style?: "none" | "primary" | "success" | "info" | "warning" | "danger";
  type: keyof typeof EFormItemType;
  max?: number | string;
  min?: number | string;
  maxLength?: number;
  minLength?: number;
  rowLength: number;
  placeholder?: string;
  step?: number | string;
  label: string;
  labelWidth?: number;
  labelHidden?: boolean;
  defaultValue?: any;
  required: boolean = false;
  options: IFormOptions[] = [];
  selectMulti?: boolean;
  optionWidth: number = 100;
  inQuery?: boolean = false;
  inEdit: boolean = true;
  inList?: boolean = false;
  showType?: keyof typeof EFormItemShowType;
  sortable?: boolean;
  dict?: string;
  dictHidden?: string;
  span: number;
  id: number;
  index: number = 0;
  children: IFormItem[] = [];

  constructor(options: IFormItem) {
    const randomNumber = Math.floor(Math.random() * 1000000);
    // Assign the random number to the desired property
    this.index = 0;
    this.id = randomNumber;
    this.key = options.type + randomNumber.toString();
    this.type = options.type;
    this.label = EFormItemType[options.type];
    this.span = 24;
    this.rowLength = 1;
    // 设置其他默认值
    switch (options.type) {
      case "textarea":
        this.rowLength = 2;
      case "input":
      case "password":
        this.maxLength = 50;
        this.minLength = 0;
        break;
      case "number":
        this.step = 1;
        break;
      case "checkbox":
      case "radio":
      case "select":
        this.options = [
          { index: 0, label: "选项1", value: 1, show: true },
          { index: 1, label: "选项2", value: 2, show: true },
        ];
        this.labelWidth = 80;
        break;
      case "switch":
        break;
      case "text":
      case "arrayContainer":
      case "layoutContainer":
        this.labelHidden = true;
        break;
      default:
        break;
    }
  }
}

export function isContainer(item: IFormItem) {
  return (
    item.type !== "text" && Object.keys(EFormItemContainer).includes(item.type)
  );
}
export function getAliasValue(pathList: string[], sourceParams: any) {
  const result = pathList.reduce((acc, cur) => {
    const realCur = Number(cur) ? Number(cur) : cur;
    if (acc) {
      return acc[realCur];
    } else {
      if (typeof realCur === "number") {
        acc = [];
        acc[cur] = {};
      } else {
        acc = {};
        acc[cur] = null;
      }
    }
  }, sourceParams);
  return result;
}
export function setAliasValue(
  newValue: any,
  pathList: string[],
  sourceParams: any
) {
  return pathList.reduce((acc, cur, index) => {
    const realCur = Number(cur) ? Number(cur) : cur;
    if (typeof realCur === "number") {
      if (!acc) {
        acc = [];
      }
      if (!acc[cur]) {
        acc[cur] = {};
      }
    } else {
      if (!acc) {
        acc = {};
      }
      if (!acc[cur]) {
        acc[cur] = null;
      }
    }
    if (index === pathList.length - 1) {
      acc![realCur] = newValue; // 修改最终的属性值
    }
    return acc[realCur];
  }, sourceParams);
}
